//
//  NSObject+VTValue.m
//  vTeam
//
//  Created by zhang hailong on 13-12-30.
//  Copyright (c) 2013年 hailong.org. All rights reserved.
//

#import "NSObject+VTValue.h"

@implementation NSObject (VTValue)

-(NSString *) vt_stringValueForKey:(NSString *) key defaultValue:(NSString *)defalutValue{
    id value = [self vt_objectValueForKey:key];
    if([value isKindOfClass:[NSString class]]){
        return value;
    }
    else if([value respondsToSelector:@selector(stringValue)]){
        value = [value stringValue];
        if(value){
            return value;
        }
    }
    else if(value){
        return [NSString stringWithFormat:@"%@",value];
    }
    return defalutValue;
}

-(NSString *) vt_stringValueForKey:(NSString *) key{
    return [self vt_stringValueForKey:key defaultValue:nil];
}

-(BOOL) vt_booleanValueForKey:(NSString *) key defaultValue:(BOOL)defalutValue{
    id value = [self vt_objectValueForKey:key];
    if([value respondsToSelector:@selector(boolValue)]){
        return [value boolValue];
    }
    return defalutValue;
}

-(BOOL) vt_booleanValueForKey:(NSString *) key{
    return [self vt_booleanValueForKey:key defaultValue:NO];
}

-(int) vt_intValueForKey:(NSString *) key defaultValue:(int)defalutValue{
    id value = [self vt_objectValueForKey:key];
    if([value respondsToSelector:@selector(intValue)]){
        return [value intValue];
    }
    return defalutValue;
}

-(int) vt_intValueForKey:(NSString *) key{
    return [self vt_intValueForKey:key defaultValue:0];
}

-(long) vt_longValueForKey:(NSString *) key defaultValue:(long)defalutValue{
    id value = [self vt_objectValueForKey:key];
    if([value respondsToSelector:@selector(longValue)]){
        return [value longValue];
    }
    return defalutValue;
}

-(long) vt_longValueForKey:(NSString *) key{
    return [self vt_longValueForKey:key defaultValue:0];
}

-(long long) vt_longLongValueForKey:(NSString *) key defaultValue:(long long)defalutValue{
    id value = [self vt_objectValueForKey:key];
    if([value respondsToSelector:@selector(longLongValue)]){
        return [value longLongValue];
    }
    return defalutValue;
}

-(long long) vt_longLongValueForKey:(NSString *) key{
    return [self vt_longLongValueForKey:key defaultValue:0];
}

-(float) vt_floatValueForKey:(NSString *) key defaultValue:(float)defalutValue{
    id value = [self vt_objectValueForKey:key];
    if([value respondsToSelector:@selector(floatValue)]){
        return [value floatValue];
    }
    return defalutValue;
}

-(float) vt_floatValueForKey:(NSString *)key{
    return [self vt_floatValueForKey:key defaultValue:0.0f];
}

-(double) vt_doubleValueForKey:(NSString *) key defaultValue:(double)defalutValue{
    id value = [self vt_objectValueForKey:key];
    if([value respondsToSelector:@selector(doubleValue)]){
        return [value doubleValue];
    }
    return defalutValue;
}

-(double) vt_doubleValueForKey:(NSString *) key{
    return [self vt_doubleValueForKey:key defaultValue:0.0];
}

-(id) vt_objectValueForKey:(NSString *) key defaultValue:(id)defalutValue{
    
    if([self isKindOfClass:[NSNull class]]
       || [self isKindOfClass:[NSString class]]
       || [self isKindOfClass:[NSNumber class]]
       || key == nil){
        return defalutValue;
    }
    else if([key hasPrefix:@"@"] && [self isKindOfClass:[NSArray class]]){
        if([key isEqualToString:@"@first"]){
            return [self vt_objectValueAtIndex:0];
        }
        else if([key isEqualToString:@"@last"]){
            return [(NSArray *) self lastObject];
        }
        else if([key isEqualToString:@"@joinString"]){
            return [(NSArray *) self componentsJoinedByString:@","];
        }
        else {
            return [self vt_objectValueAtIndex:[[key substringFromIndex:1] intValue]];
        }
    }
    @try {
        return [self valueForKey:key];
    }
    @catch (NSException *exception) {
        
    }
    return defalutValue;
}

-(id) vt_objectValueForKey:(NSString *)key{
    return [self vt_objectValueForKey:key defaultValue:nil];
}

-(NSDictionary *) vt_dictionaryValueForKey:(NSString *)key{
    id v = [self vt_objectValueForKey:key];
    if([v isKindOfClass:[NSDictionary class]]){
        return v;
    }
    return nil;
}

-(NSArray *) vt_arrayValueForKey:(NSString *)key{
    id v = [self vt_objectValueForKey:key];
    if([v isKindOfClass:[NSArray class]]){
        return v;
    }
    return nil;
}


-(NSString *) vt_stringValueForKeyPath:(NSString *) keyPath defaultValue:(NSString *)defalutValue{
    id value = [self vt_objectValueForKeyPath:keyPath];
    if([value isKindOfClass:[NSString class]]){
        return value;
    }
    else if([value respondsToSelector:@selector(stringValue)]){
        value = [value stringValue];
        if(value){
            return value;
        }
    }
    else if(value){
        return [NSString stringWithFormat:@"%@",value];
    }
    return defalutValue;
}

-(NSString *) vt_stringValueForKeyPath:(NSString *) keyPath{
    return [self vt_stringValueForKeyPath:keyPath defaultValue:nil];
}

-(BOOL) vt_booleanValueForKeyPath:(NSString *) keyPath defaultValue:(BOOL)defalutValue{
    id value = [self vt_objectValueForKeyPath:keyPath];
    if([value respondsToSelector:@selector(boolValue)]){
        return [value boolValue];
    }
    return defalutValue;
}

-(BOOL) vt_booleanValueForKeyPath:(NSString *)keyPath{
    return [self vt_booleanValueForKeyPath:keyPath defaultValue:NO];
}

-(int) vt_intValueForKeyPath:(NSString *) keyPath defaultValue:(int)defalutValue{
    id value = [self vt_objectValueForKeyPath:keyPath];
    if([value respondsToSelector:@selector(intValue)]){
        return [value intValue];
    }
    return defalutValue;
}

-(int) vt_intValueForKeyPath:(NSString *) keyPath{
    return [self vt_intValueForKeyPath:keyPath defaultValue:0];
}

-(long) vt_longValueForKeyPath:(NSString *) keyPath defaultValue:(long)defalutValue{
    id value = [self vt_objectValueForKeyPath:keyPath];
    if([value respondsToSelector:@selector(longValue)]){
        return [value longValue];
    }
    return defalutValue;
}

-(long) vt_longValueForKeyPath:(NSString *) keyPath{
    return [self vt_longValueForKeyPath:keyPath defaultValue:0];
}

-(long long) vt_longLongValueForKeyPath:(NSString *) keyPath defaultValue:(long long)defalutValue{
    id value = [self vt_objectValueForKeyPath:keyPath];
    if([value respondsToSelector:@selector(longLongValue)]){
        return [value longLongValue];
    }
    return defalutValue;
}

-(long long) vt_longLongValueForKeyPath:(NSString *)keyPath{
    return [self vt_longLongValueForKeyPath:keyPath defaultValue:0];
}

-(float) vt_floatValueForKeyPath:(NSString *) keyPath defaultValue:(float)defalutValue{
    id value = [self vt_objectValueForKeyPath:keyPath];
    if([value respondsToSelector:@selector(floatValue)]){
        return [value floatValue];
    }
    return defalutValue;
}

-(float) vt_floatValueForKeyPath:(NSString *) keyPath{
    return [self vt_floatValueForKeyPath:keyPath defaultValue:0.0f];
}

-(double) vt_doubleValueForKeyPath:(NSString *) keyPath defaultValue:(double)defalutValue{
    id value = [self vt_objectValueForKeyPath:keyPath];
    if([value respondsToSelector:@selector(doubleValue)]){
        return [value doubleValue];
    }
    return defalutValue;
}

-(double) vt_doubleValueForKeyPath:(NSString *) keyPath{
    return [self vt_doubleValueForKeyPath:keyPath defaultValue:0.0];
}

-(id) vt_objectValueForKeyPath:(NSString *) keyPath defaultValue:(id)defalutValue{
    
    if([self isKindOfClass:[NSNull class]]
       || [self isKindOfClass:[NSString class]]
       || [self isKindOfClass:[NSNumber class]]
       || keyPath == nil){
        return defalutValue;
    }
    
    @try {
        return [self valueForKeyPath:keyPath];
    }
    @catch (NSException *exception) {
        
    }
    return defalutValue;
}

-(id) vt_objectValueForKeyPath:(NSString *) keyPath{
    return [self vt_objectValueForKeyPath:keyPath defaultValue:nil];
}

-(NSDictionary *) vt_dictionaryValueForKeyPath:(NSString *)keyPath{
    id v = [self vt_objectValueForKeyPath:keyPath];
    if([v isKindOfClass:[NSDictionary class]]){
        return v;
    }
    return nil;
}

-(NSArray *) vt_arrayValueForKeyPath:(NSString *)keyPath{
    id v = [self vt_objectValueForKeyPath:keyPath];
    if([v isKindOfClass:[NSArray class]]){
        return v;
    }
    return nil;
}

-(void) vt_setObjectValue:(id) value forKey:(NSString *) key{
    
    @try {
        [self setValue:value forKey:key];
    }
    @catch (NSException *exception) {
    }
}

-(void) vt_setObjectValue:(id) value forKeyPath:(NSString *) keyPath{
    
    @try {
        [self setValue:value forKeyPath:keyPath];
    }
    @catch (NSException *exception) {
    }
}

-(NSArray *) vt_arrayValue{
    if([self isKindOfClass:[NSArray class]]){
        return (NSArray *) self;
    }
    return nil;
}

-(NSDictionary *) vt_dictionaryValue{
    if([self isKindOfClass:[NSDictionary class]]){
        return (NSDictionary *) self;
    }
    return nil;
}

-(id) vt_objectValueAtIndex:(NSUInteger) index{
    if([self isKindOfClass:[NSArray class]] && index < [(NSArray *)self count]){
        return [(NSArray *) self objectAtIndex:index];
    }
    return nil;
}

#pragma mark - mutableCopy
- (id)vt_dictionaryValueMutableCopy
{
    if ([self isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary * mDic = [[NSMutableDictionary alloc] initWithDictionary:(NSDictionary *)self];
        for (id key in mDic.allKeys) {
            id value = mDic[key];
            if ([value isKindOfClass:[NSDictionary class]]) {
                value = [value vt_dictionaryValueMutableCopy];
                mDic[key] = value;
            }else if ([value isKindOfClass:[NSArray class]]){
                value = [value vt_arrayValueMutableCopy];
                mDic[key] = value;
            }
        }
        return [mDic autorelease];
    }
    return nil;
}

- (id)vt_arrayValueMutableCopy
{
    if ([self isKindOfClass:[NSArray class]]) {
        NSMutableArray * mArr = [[NSMutableArray alloc] initWithArray:(NSArray *)self];
        for (NSInteger i = 0; i < mArr.count; i++) {
            id value = mArr[i];
            if ([value isKindOfClass:[NSDictionary class]]) {
                value = [value vt_dictionaryValueMutableCopy];
                mArr[i] = value;
            }else if ([value isKindOfClass:[NSArray class]]){
                value = [value vt_arrayValueMutableCopy];
                mArr[i] = value;
            }
        }
        return [mArr autorelease];
    }
    return nil;
}

@end
